<template>
    <div class="block-config-chain-container layout-padding">
        <el-card shadow="hover" class="layout-padding-auto">


            <el-card style="margin-top: 20px;" shadow="hover" class="layout-padding-auto">

                <div class="bar_tools">
                    <el-button-group class="ml-4">
                        <el-button size="default" type="primary" @click="handlerAdd()">新增配置</el-button>
                        <el-button type="primary" size="default" @click.native.prevent="handleResetSearch()">刷新</el-button>
                    </el-button-group>
                </div>
                <el-table size="default" :data="state.dataList" style="border: 1px solid #ebeef5; margin-top: 20px">

                    <el-table-column align="center" label="id" width="60">
                        <template #default="scope">
                            <span>{{ scope.row.chainConfigId }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="RPC地址">
                        <template #default="scope">
                            <span>{{ scope.row.rpcAddress }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="部署链">
                        <template #default="scope">
                            <span>{{ scope.row.chainSymbol }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="区块高度">
                        <template #default="scope">
                            <span>{{ scope.row.blockNumber }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="链ID">
                        <template #default="scope">
                            <span>{{ scope.row.chainId }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="创建时间" >
                        <template #default="scope">
                            <span>{{ formatDate(new Date(scope.row.createDate), "YYYY-mm-dd HH:MM:SS") }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="修改时间" >
                        <template #default="scope">
                            <span>{{ formatDate(new Date(scope.row.updateDate), "YYYY-mm-dd HH:MM:SS") }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="状态">
                        <template #default="scope">
                            <el-tag v-show="scope.row.chainStatus == 10" effect="dark" size="small" type="success">启用</el-tag>
                            <el-tag v-show="scope.row.chainStatus == 20" effect="dark" size="small" type="danger">禁用</el-tag>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="操作" fixed="right" >
                        <template #default="scope">
                           
                                <el-button @click="handlerEdit(scope.row)" type="primary" size="small">编辑</el-button>
                                <el-button @click="handlerUpdateStatus(scope.row.chainConfigId, scope.row.chainStatus)"
                                   v-show="scope.row.chainStatus == 10" type="danger" size="small">禁用</el-button>
                                   <el-button @click="handlerUpdateStatus(scope.row.chainConfigId, scope.row.chainStatus)"
                                   v-show="scope.row.chainStatus == 20" type="success" size="small">启用</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination_div">
                    <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
                        :pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.ruleForm.page" background
                        v-model:page-size="state.ruleForm.limit" layout="total, sizes, prev, pager, next, jumper"
                        :total="state.ruleForm.total">
                    </el-pagination>
                </div>
            </el-card>


        </el-card>

        <editdialog ref="editdialogRef" @refresh="refresh" />

    </div>
</template>


<script setup lang="ts" name="mailsendconfig">

import { formatDate } from '../../../../utils/formatTime';
import { reactive, onMounted, ref, defineAsyncComponent } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
// 导入接口
import { useChainApi } from "../../../../api/chain";



// 引入组件
const editdialog = defineAsyncComponent(() => import('./editdialog.vue'))
const editdialogRef = ref();



const state = reactive({
    dataList: [] as any,
    ruleForm: {
        page: 1,
        limit: 10,
        total: 0,
        itemInfo: {},
        chainSymbol: '',
        chainList: [],

    },
    pickerOptions: {
        shortcuts: [
            {
                text: "最近一周",
                onClick(picker: any) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(
                        start.getTime() - 3600 * 1000 * 24 * 7
                    );
                    picker.$emit("pick", [start, end]);
                },
            },
            {
                text: "最近一个月",
                onClick(picker: any) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(
                        start.getTime() - 3600 * 1000 * 24 * 30
                    );
                    picker.$emit("pick", [start, end]);
                },
            },
            {
                text: "最近三个月",
                onClick(picker: any) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(
                        start.getTime() - 3600 * 1000 * 24 * 90
                    );
                    picker.$emit("pick", [start, end]);
                },
            },
        ],
    },
    multipleSelection: [],// 选择的项


});
//新增按钮
const handlerAdd = () => {
    editdialogRef.value.openDialog("add");
}
//编辑按钮
const handlerEdit = (row: any) => {
    editdialogRef.value.openDialog("edit",row);
}
//测试按钮
const handlerUpdateStatus = async (id: any, status: any) => {

    let vo = {
        chainConfigId:id,
    } as any;
    try {
        if (status == 10) {
            vo['status'] = 20;
        } else {
            vo['status'] = 10;
        }
        await useChainApi().updateStatus(vo);
        getChainList();
    } catch (error: any) {
        ElMessage.error(error.msg);
    }

}

const handleResetSearch = () => {
    state.ruleForm = {
        page: 1,
        limit: 10,
        total: 0,
        itemInfo: {},
        chainSymbol: '',
        chainList: [],
    };
    getChainList();
}

const refresh = () => {
    getChainList();
}


// 分页改变
const onHandleSizeChange = (val: number) => {
    state.ruleForm.limit = val;
    getChainList();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
    state.ruleForm.page = val;
    getChainList();
};
const getChainList = async () => {
    try {
        let vo = {
            limit: state.ruleForm.limit,
            page: state.ruleForm.page,
            chainSymbol:state.ruleForm.chainSymbol
        } as any;

        let tempList = await useChainApi().getChainList(vo);

        state.dataList = tempList.list;
        state.ruleForm.total = tempList.total;

    } catch (error: any) {
        ElMessage.error(error.msg);
    }
}

// 页面加载时
onMounted(async () => {

    await getChainList();
});

</script>



<style lang="scss" scoped>
.el-form .el-form-item:last-of-type {
    margin-bottom: 22px !important;
}

.bar_tools {
    display: flex;
    flex-direction: row-reverse;
    margin-bottom: 10px;
}

.block-config-chain-container {
    :deep(.el-card__body) {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;

        .el-table {
            flex: 1;
        }
    }
}

.bar_btn_div {
    display: flex;
    padding: 20px;
    border: 1px solid #ebeef5;
    align-items: center;
    justify-content: space-between;
    border-radius: 5px 5px 0px 0px;

    .title_div {
        display: flex;
        font-size: 16px;

        &>div {
            margin-right: 10px;
        }
    }
}

.search_div {
    border: 1px solid #ebeef5;
    border-top: 0px;
    border-radius: 0px 0px 5px 5px;
    padding: 20px;
    padding-bottom: 0px;
}
</style>
